﻿
/*
date	 :2010-10-1
author	 :writed by wangchao
*/
Array.prototype.Remove = function (n) {
    if (n < 0)
        return this;
    else
        return this.slice(0, n).concat(this.slice(n + 1, this.length));
}

var clickEvents = new Array();
var xmlDom = null;
var lastClickTarget = null;

function CloseWin() {
    window.opener = null;
    window.open('', '_self');
    window.close();
}
function SelectAll(chkAll) {
    var inputs = document.forms[0].elements;
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'checkbox') {
            inputs[i].checked = chkAll.checked;
        }
    }
}
function BindClick(id, fn, param) {
    var e = new Object();
    e.id = id;
    e.func = fn;
    e.param = param;
    clickEvents.push(e);
}
function UnbindClick(idtobeunbind) {
    for (var i in clickEvents) {
        if (clickEvents[i].id == idtobeunbind) {
            clickEvents.Remove(i);
            break;
        }
    }
}
function GetLastClickTarget() {
    return lastClickTarget;
}
/*get a target from window.event*/
function GetTarget(e) {
    if (e == null || typeof (e) == "undefined")
        e = window.event;
    return ((typeof e.target != "undefined") ? e.target : e.srcElement);
}
/*catch mouse click*/
function OnMouseDown(e) {
    if (e == null || typeof (e) == "undefined")
        e = window.event;
    var temp = GetTarget(e);
    if (temp == null)
        return false;
    lastClickTarget = temp;
    s = temp.id;
    for (var i in clickEvents) {
        if (s == clickEvents[i].id) {
            clickEvents[i].func(clickEvents[i].param);
            break;
        }
    }
}
function AddMouseDownListener(fn) {
    var oldfn = document.onmousedown;
    if (typeof oldfn != 'function') {
        document.onmousedown = fn;
    }
    else {
        document.onmousedown = function () { oldfn(); fn(); };
    }
}

function AddLoadListener(fn) {
    if (typeof window.addEventListener != 'undefined') {
        window.addEventListener('load', fn, false);
    }
    else if (typeof document.addEventListener != 'undefined') {
        document.addEventListener('load', fn, false);
    }
    else if (typeof window.attachEvent != 'undefined') {
        window.attachEvent('onload', fn);
    }
    else {
        var oldfn = window.onload;
        if (typeof oldfn != 'function') {
            window.onload = fn;
        }
        else {
            window.onload = function () { oldfn(); fn(); };
        }
    }
}
//fast get object
function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}
//xmldom
function GetXMLDOM() {
    if (xmlDom)
        return xmlDom;
    if (window.ActiveXObject) {
        //ie
        xmlDom = new ActiveXObject("Microsoft.XMLDOM");
    }
    else if (document.implementation.createDocument) {
        //Mozilla, Firefox, Opera, etc.
        xmlDom = document.implementation.createDocument("", "", null);
    }
    xmlDom.async = false;

    return xmlDom;
}
//load xml
function LoadXML(url) {
    xmlDom = GetXMLDOM();
    xmlDom.load(url);
    return xmlDom;
}
//MakeRequest
function MakeRequest(url, method, data, request_fun) {
    var http_request = new CRequest();
    http_request.Request(url, method, data, request_fun);
}

function Sleep(n) {
    var start = new Date().getTime();
    while (true) if (new Date().getTime() - start > n) break;
}
/* cookie operations */
function ReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return "";
}

function IfExistedJS(src) {
    scriptArry = document.getElementsByTagName('script');
    for (var i in scriptArry) 
    {
        if (i.src == src) 
        {
            return true;
        }
    }
    return false;
}

/*
1.if js is function 
    1.if function in this file 
    2.if function not in this file
2.if js is js
*/
function ExcuteObj(obj) {
    if(obj==null||obj=="")
	return;
    if ( typeof obj == "function") {
        obj();
	return;
    }
    
	  var str=obj;	
          try
          {
	    var reg=new RegExp(/^[a-zA-Z0-9|_]{1,20}$/);  
	    if (reg.test(str)) 
            {
		 eval(str+"();")
            }
	    else
	    {
	    	 eval(str);
	    }
        }
        catch (e)
        {
            alert(e.message);
        }
}
function BestLoadScript(src, js) {
    //if existed
    if (IfExistedJS(src)) {
        return; //warning!if existed js,callback fun won't be called.
    }
    //if not existed
    var script = document.createElement("script")
    script.type = "text/javascript";
    if (script.readyState) { //IE
        script.onreadystatechange = function () {
            if (script.readyState == "loaded" ||script.readyState == "complete") {
                script.onreadystatechange = null;
                ExcuteObj(js);
            }
    	}
    }
    else 
    { 
      //Others
      script.onload = function()
      {
         ExcuteObj(js);
       };
    }
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
}
/*
src     :js file name,if not existed,than will be loaded.
callback:function will be called;
*/
function CallEx(src, js) {
    //if existed
    if (IfExistedJS(src)) {
            ExcuteObj(js);
        }
    else {
        BestLoadScript(src, js);
    }
}
//GOOGLE ANALY
function OnGoogleAnnlyScriptLoaded() {
    try {
        var pageTracker = _gat._getTracker("UA-2826582-3");
        pageTracker._trackPageview();
    }
    catch (err) { }
}

function WebAnalytics() {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    var url1 = unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E");
    var url2 = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    BestLoadScript(url1, null);
    BestLoadScript(url2, OnGoogleAnnlyScriptLoaded);
}



